<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>solr7配置同义词 - (power up)</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Kevin Jiang" /><meta name="description" content="Solr的同义词可以满足绝大部分的需求，可以配置双向，也可以配置单向的同义词列表，功能非常的强大。 Solr的同义词可以在两个方面设置，一是索" /><meta name="keywords" content="KevinJiang, AI大模型, AI落地, 全栈工程师, Java, Spring Boot" />






<meta name="generator" content="Hugo 0.84.4 with theme even" />


<link rel="canonical" href="http://kevinjiang.info/post/searchengine/solr7%E9%85%8D%E7%BD%AE%E5%90%8C%E4%B9%89%E8%AF%8D/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.78f8f17bab244b9ee62ad16480c9584d5fc2db06ae20681d1ca225cefd80767c.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="solr7配置同义词" />
<meta property="og:description" content="Solr的同义词可以满足绝大部分的需求，可以配置双向，也可以配置单向的同义词列表，功能非常的强大。 Solr的同义词可以在两个方面设置，一是索" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://kevinjiang.info/post/searchengine/solr7%E9%85%8D%E7%BD%AE%E5%90%8C%E4%B9%89%E8%AF%8D/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2018-01-10T14:59:47+00:00" />
<meta property="article:modified_time" content="2018-01-10T14:59:47+00:00" />

<meta itemprop="name" content="solr7配置同义词">
<meta itemprop="description" content="Solr的同义词可以满足绝大部分的需求，可以配置双向，也可以配置单向的同义词列表，功能非常的强大。 Solr的同义词可以在两个方面设置，一是索"><meta itemprop="datePublished" content="2018-01-10T14:59:47+00:00" />
<meta itemprop="dateModified" content="2018-01-10T14:59:47+00:00" />
<meta itemprop="wordCount" content="1785">
<meta itemprop="keywords" content="Solr," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="solr7配置同义词"/>
<meta name="twitter:description" content="Solr的同义词可以满足绝大部分的需求，可以配置双向，也可以配置单向的同义词列表，功能非常的强大。 Solr的同义词可以在两个方面设置，一是索"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">(Power up)</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/resume/">
        <li class="mobile-menu-item">简历</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">(Power up)</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/resume/">简历</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">solr7配置同义词</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-01-10 </span>
        
          <span class="more-meta"> 约 1785 字 </span>
          <span class="more-meta"> 预计阅读 4 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#查询时使用同义词">查询时使用同义词</a></li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>Solr的同义词可以满足绝大部分的需求，可以配置双向，也可以配置单向的同义词列表，功能非常的强大。</p>
<!-- raw HTML omitted -->
<p>Solr的同义词可以在两个方面设置，一是索引的时候；二是查询的时候；或者你同时设置两者，同时设置的时候会非常复杂，会产生意想不到的结果。</p>
<h1 id="先从基本配置开始">先从基本配置开始</h1>
<p>假设我们有同义词列表<code>synonyms_en.txt</code></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">ability,capability
</code></pre></td></tr></table>
</div>
</div><p>配置Solr的<code>fieldtype</code></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">&lt;fieldType name=&#34;text_en&#34; class=&#34;solr.TextField&#34; positionIncrementGap=&#34;100&#34;&gt;
  &lt;analyzer type=&#34;index&#34;&gt;
    &lt;tokenizer class=&#34;solr.StandardTokenizerFactory&#34;/&gt;
    &lt;!-- synonym --&gt;
    &lt;filter class=&#34;solr.SynonymGraphFilterFactory&#34; synonyms=&#34;synonyms_en.txt&#34; ignoreCase=&#34;true&#34; expand=&#34;false&#34;/&gt;
    ......
  &lt;/analyzer&gt;
  &lt;analyzer type=&#34;query&#34;&gt;
    ......
  &lt;/analyzer&gt;
&lt;/fieldType&gt;
</code></pre></td></tr></table>
</div>
</div><p>先尝试索引时候设置同义词，直接在<code>analyzer type=&quot;index&quot;</code>内部配置即可，<code>SynonymGraphFilterFactory</code>暂时还是一个试验性质的功能，以后的版本可能会有所改动。class还可以设置成<code>solr.SynonymFilterFactory</code></p>
<p>Solr自带的同义词配置有很多的参数：</p>
<ol>
<li>synonyms：指定同义词列表文件</li>
<li>ignoreCase：顾名思义是忽略大小写</li>
<li>expand：是否扩展同义词，意思是a和b是同义词，如果设置为true，那么搜索a的时候，同时搜索a和b，如果设置为false，那么搜索a的时候就搜索a，搜索b的时候也搜索a。也就是说单向双向的概念：如果设置为true，那么就是双向的；如果设置为false就是单向的，后面的词会转换成前面的词搜索。</li>
<li>tokenizerFactory：指定同义词列表使用的分词器</li>
<li>dedup：是否忽略重复，true为忽略</li>
</ol>
<p>根据上面的设置，如果搜索<code>ability</code>，那么最终搜索的也是<code>ability</code>。如果搜索的是<code>capability</code>，最终搜索的时候会把<code>capability</code>转换成<code>ability</code>搜索。这样就达到了同义词搜索的功能。</p>
<h2 id="查询时使用同义词">查询时使用同义词</h2>
<p>只需要把index部分的配置移到query部分即可，如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">&lt;fieldType name=&#34;text_en&#34; class=&#34;solr.TextField&#34; positionIncrementGap=&#34;100&#34;&gt;
  &lt;analyzer type=&#34;index&#34;&gt;
    ......
  &lt;/analyzer&gt;
  &lt;analyzer type=&#34;query&#34;&gt;
    &lt;tokenizer class=&#34;solr.StandardTokenizerFactory&#34;/&gt;
    &lt;!-- synonym --&gt;
    &lt;filter class=&#34;solr.SynonymGraphFilterFactory&#34; synonyms=&#34;synonyms_en.txt&#34; ignoreCase=&#34;true&#34; expand=&#34;false&#34;/&gt;
    ......
  &lt;/analyzer&gt;
&lt;/fieldType&gt;
</code></pre></td></tr></table>
</div>
</div><p>如果这时候把expand设置成true，那么搜索任何一个词，最终都会转换成搜索：<code>SynonymQuery(Synonym(field:ability field:capability))</code>。注意你看到的可能会是<code>SynonymQuery(Synonym(title.en:abil title.en:capabl))</code>，这是使用了porter stemming之后的结果。</p>
<p>我的请求语句如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">http://192.168.0.106:8983/solr/lnc/select?debug=query&amp;df=title.en&amp;q=capability
</code></pre></td></tr></table>
</div>
</div><h1 id="单向双向概念">单向，双向概念</h1>
<p>这两个概念弄得还是有点模糊。第一：如果设置expand=false，那么同义词都会转换成第一个词进行索引（或者查询）；第二：如果设置expand=true，那么一个词会扩展成多个词同时查询；第三：使用<code>ability =&gt; capability</code>的配置时，如果查询<code>ability</code>，不管expand是true还是false，都会转换成对<code>capability</code>的搜索。</p>
<p>我理解的单向可以做成下面的列表：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">ability =&gt; capability
ability =&gt; ability
</code></pre></td></tr></table>
</div>
</div><p>这样搜索<code>ability</code>的时候，搜索会变成：<code>SynonymQuery(Synonym(field:ability field:capability))</code>，而搜索<code>capability</code>的时候，只会搜索<code>capability</code>。</p>
<p>通过这样的设置来达到单向同义词的目标。</p>
<h1 id="中文分词为多个词的配置">中文分词为多个词的配置</h1>
<p>这个问题的由来是这样的，假设我们使用的分词器叫<code>ABCTokenizerFactory</code>。现在有下面的同义词列表<code>synonyms_cn.txt</code>：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">中华人民共和国,中国
</code></pre></td></tr></table>
</div>
</div><p>这里假设<code>中华人民共和国</code>分词为<code>中华/人民/共和国</code>三个词</p>
<p>schema如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">&lt;fieldType name=&#34;text_cn&#34; class=&#34;solr.TextField&#34; positionIncrementGap=&#34;100&#34;&gt;
    &lt;analyzer type=&#34;index&#34;&gt;
        &lt;tokenizer class=&#34;ABCTokenizerFactory&#34;/&gt;
        ...
    &lt;/analyzer&gt;
    &lt;analyzer type=&#34;query&#34;&gt;
        &lt;tokenizer class=&#34;ABCTokenizerFactory&#34;/&gt;
        &lt;filter class=&#34;solr.SynonymGraphFilterFactory&#34;
                synonyms=&#34;synonyms_cn.txt&#34;
                ignoreCase=&#34;true&#34;
                expand=&#34;true&#34;/&gt;
        ...
    &lt;/analyzer&gt;
&lt;/fieldType&gt;
</code></pre></td></tr></table>
</div>
</div><p>这样配置的时候，你搜索<code>中华人民共和国</code>是没有办法匹配到<code>中国</code>的。因为同义词有自己的分词器，如果这两个分词器的分词结果不相同，那么这个同义词设置基本上就是无效的，Synonyms的源码也可以体现这一点：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">public class SolrSynonymParser extends SynonymMap.Parser {
  private final boolean expand;
  
  public SolrSynonymParser(boolean dedup, boolean expand, Analyzer analyzer) {
    super(dedup, analyzer);
    this.expand = expand;
  }
}
</code></pre></td></tr></table>
</div>
</div><p>可以看到有一个analyzer，这个analyzer就是对同义词列表进行分词用的。</p>
<p>所以需要设置synonym的tokenizerFactory。如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">&lt;fieldType name=&#34;text_cn&#34; class=&#34;solr.TextField&#34; positionIncrementGap=&#34;100&#34;&gt;
    &lt;analyzer type=&#34;index&#34;&gt;
        &lt;tokenizer class=&#34;ABCTokenizerFactory&#34;/&gt;
        ...
    &lt;/analyzer&gt;
    &lt;analyzer type=&#34;query&#34;&gt;
        &lt;tokenizer class=&#34;ABCTokenizerFactory&#34;/&gt;
        &lt;filter class=&#34;solr.SynonymGraphFilterFactory&#34;
                synonyms=&#34;synonyms_cn.txt&#34;
                ignoreCase=&#34;true&#34;
                expand=&#34;true&#34;
                tokenizerFactory=&#34;ABCTokenizerFactory&#34;/&gt;
        ...
    &lt;/analyzer&gt;
&lt;/fieldType&gt;
</code></pre></td></tr></table>
</div>
</div><p>这样就解决了分词为多个的问题了。</p>
<h1 id="总结">总结</h1>
<ol>
<li>基本用法，可以直接设置到只有一个analyzer节点的fieldtype中，这是同时对查询和索引生效</li>
<li>单向双向的问题</li>
<li>中文多分词的问题</li>
</ol>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Kevin Jiang</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2018-01-10
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank">CC BY-NC-ND 4.0</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/wechat_pay_1242x1242.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/alipay_600x600.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/solr/">Solr</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/react%E5%88%9D%E5%A7%8B%E9%85%8D%E7%BD%AE/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">react初始配置</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/searchengine/solr7%E4%BB%8E%E6%90%AD%E5%BB%BA%E5%88%B0%E5%85%A5%E5%9D%91/">
            <span class="next-text nav-default">Solr7从搭建到入坑</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wenlin1988@126.com" class="iconfont icon-email" title="email"></a>
      <a href="https://www.linkedin.com/in/%E6%96%87%E6%9E%97-%E8%92%8B-0a3204126/" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="https://github.com/kevindragon" class="iconfont icon-github" title="github"></a>
  <a href="http://kevinjiang.info/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy;
    2015 -
    2023<span class="heart"><i class="iconfont icon-heart"></i></span><span>Kevin Jiang</span>
    <a href="https://beian.miit.gov.cn/" target="_blank">湘ICP备2022022745号</a>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        tags: 'ams',
        }
    };
  </script>
  <script type="text/javascript" async src="/lib/mathjax/es5/tex-mml-chtml.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?b73ff6d4afc4af9e582d8a5dc068bab9";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>






</body>
</html>
